package cn.shadow.game.chess.CHnchess.chessman;


import cn.shadow.game.chess.CHnchess.board.Board;
import cn.shadow.game.chess.CHnchess.board.Position;
import cn.shadow.game.chess.CHnchess.board.Slash;
import cn.shadow.game.chess.CHnchess.type.Camp;
import cn.shadow.game.chess.CHnchess.type.ChessType;

public class Ma extends AbstractChess implements Slash {

    public Ma(Board board, Camp camp) {
        super(ChessType.MA, board, camp);
    }



    /**
     * @param br 障碍点r
     * @param bc 障碍 c
     * @param pr 点r
     * @param pc 点c
     */
    private void doAdd(int br, int bc, int pr, int pc) {
        Position barrierPoint = getBoard().getPoint(br, bc);
        Position point = getBoard().getPoint(pr, pc);
        if (barrier(barrierPoint)) {
            return;
        }
        if (point != null) {
            addPoint(point);
        }
    }

    private void addPoint(Position point) {
        Chess chess = point.getChess();
        if (chess != null) {
            if (chess.getCamp() == this.getCamp()) {
                return;
            }
        }
        if (point.isOverRanged()) {
            return;
        }
        canSetPosition.add(point);
        canAttackPosition.add(point);
    }


    private boolean barrier(Position point) {
        if (point == null) {
            return false;
        }
        Chess chess = point.getChess();
        return chess != null;
    }

    /**
     * 左上
     */
    @Override
    public void addLeftUpPoint() {
        int row = this.getRow();
        int col = this.getCol();
        doAdd(row - 1, col, row - 2, col - 1);
        doAdd(row, col - 1, row - 1, col - 2);
    }

    /**
     * 右上
     */
    @Override
    public void addRightUpPoint() {
        int row = getRow();
        int col = getCol();
        doAdd(row - 1, col, row - 2, col + 1);
        doAdd(row, col + 1, row - 1, col + 2);
    }

    /**
     * 左下
     */
    @Override
    public void addLeftDownPoint() {
        int row =getRow();
        int col =getCol();
        doAdd(row + 1, col, row + 2, col - 1);
        doAdd(row, col - 1, row + 1, col - 2);
    }

    /**
     * 右下
     */
    @Override
    public void addRightDownPoint() {
        int row = getRow();
        int col = getCol();
        doAdd(row + 1, col, row + 2, col + 1);
        doAdd(row, col + 1, row + 1, col + 2);
    }
}
